package com.sjhang.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shenmazong.vo.ListParamVo;
import com.sjhang.entity.TbOutline;
import com.sjhang.mapper.TbOutlineMapper;
import com.sjhang.service.TbOutlineService;
import com.sjhang.utils.ResultResponse;
import com.sjhang.vo.OutLineVo;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 大纲表
 服务实现类
 *
 */
@Service
public class TbOutlineServiceImpl extends ServiceImpl<TbOutlineMapper, TbOutline> implements TbOutlineService {

    @Resource
    private TbOutlineMapper tbOutlineMapper;

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    /***
     * @description 对阶段新增某一个大纲
     */
    @Override
    public ResultResponse insertTbOutLine(TbOutline listParamVo) {
        tbOutlineMapper.insert( listParamVo);
        return ResultResponse.SUCCESS();
    }

    /***
     * @description 对大纲名称进行修改
     */
    @Override
    public ResultResponse updateTboutLine(TbOutline listParamVo) {
        tbOutlineMapper.updateById(listParamVo);
        return ResultResponse.SUCCESS();
    }

    /***
     * @description 删除某一个大纲
     */
    @Override
    public ResultResponse deleteTboutLine(Long listParamVo) {
        tbOutlineMapper.deleteById(listParamVo);
        return ResultResponse.SUCCESS();
    }

    /***
     * @description 查询所有大纲
     */
    @Override
    public ResultResponse selectTBOutline(ListParamVo listParamVo) {
        IPage<OutLineVo> page = this.tbOutlineMapper.selectOutLinePage(
                new Page<>(listParamVo.getCurrent(),listParamVo.getSize()),
                listParamVo.getParamMap().get("stageID")
        );
        return ResultResponse.SUCCESS(page);
    }

    /**
     * 定时导入es
     * @return
     */
    @Scheduled(fixedRate = 600000L)
    public void dbToEs() {
        List<TbOutline> tbOutlines = this.tbOutlineMapper.selectList(null);
        if(tbOutlines.size() > 0){
            elasticsearchRestTemplate.save(tbOutlines);
        }
    }


}
